//
//  CZPLViewController.swift
//  GZWeibo007
//
//  Created by 易辉华 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

import SVProgressHUD

class CZPLViewController: UIViewController {
    // MARK: - 属性
    /// 微博模型
    var status: CZStatus?
    init(status: CZStatus) {
    super.init(nibName: nil, bundle: nil)
    self.status = status
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        self.title = "评论微博"
        addChildView()
    }
  private func prepareUI() {
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "Cancel")
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "commentStatus")
    }
    //评论微博
    func commentStatus() {
    //获取textView的文本内容发送给服务器
    let text = textView.text
        if text == nil || text == "" {
        SVProgressHUD.showInfoWithStatus("内容不能为空")
            return
        }else {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                SVProgressHUD.showWithStatus("正在评论微博...", maskType: SVProgressHUDMaskType.Black)
                CZNetworkTool.sharedInstance.commentStatus(self.status!.id, comment: text, finished: { (result, error) -> () in
                    if error != nil {
                        print("error: \(error)")
                        SVProgressHUD.showErrorWithStatus("网络不给力...", maskType: SVProgressHUDMaskType.Black)
                        return
                    }
                    // 发送成功, 直接关闭界面
                    print("评论微博成功:\(result)")
                    self.Cancel()
                })

            })
            
        }
    }
    func Cancel() {
    textView.resignFirstResponder()
        // 关闭sv提示
        SVProgressHUD.dismiss()
    dismissViewControllerAnimated(true, completion: nil)
    }
    func addChildView() {
        //添加控件
        view.addSubview(textView)
        
        //设置约束
        textView.ff_Fill(view)
        //设置Title
        let title = "评论微博"
        let name = CZUserAccount.loadUserAccount()?.screen_name ?? "没获取到用户名"
        // 有用户名
        let titleName = title + "\n" + name
        
        // 创建可变的属性文本
        let attrString = NSMutableAttributedString(string: titleName)
        
        // 创建label
        let label = UILabel()
        
        // 设置属性文本
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        
        // 获取NSRange
        let nameRange = (titleName as NSString).rangeOfString(name)
        
        // 设置属性文本的属性
        attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: nameRange)
        attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.lightGrayColor(), range: nameRange)
        
        // 顺序不要搞错
        label.attributedText = attrString
        label.sizeToFit()
        
        navigationItem.titleView = label

    }

    private lazy var textView: UITextView = {
        let textView = UITextView()
        // 当textView被拖动的时候就会将键盘退回,textView能拖动
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        textView.font = UIFont.systemFontOfSize(18)
        textView.backgroundColor = UIColor(white: 0.85, alpha: 1)
        textView.textColor = UIColor.blackColor()
        textView.bounces = true
        textView.alwaysBounceVertical = true
        
        return textView
    }()

    
}
